package com.sky.service.impl;

import com.sky.Entity.Dish;
import com.sky.Entity.ShoppingCart;
import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import com.sky.vo.SetmealVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;
    @Override
    @Transactional
    public void add(ShoppingCartDTO shoppingCartDTO) {
        Long dishId = shoppingCartDTO.getDishId();
        Long setmealId = shoppingCartDTO.getSetmealId();
        ShoppingCart cart=new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,cart);
        cart.setUserId(BaseContext.getEmpId());




        String name=null;
        String image=null;
        BigDecimal amount=new BigDecimal(0);
        if (dishId != null) {
            //菜品加购物车
            Dish dish = dishMapper.queryById(dishId);
            name=dish.getName();
            image=dish.getImage();
            amount=new BigDecimal(dish.getPrice());

        }else if(setmealId!=null){
            //套餐加购
            SetmealVo setmealVo = setmealMapper.queryById(setmealId);
            name=setmealVo.getName();
            image=setmealVo.getImage();
            amount=new BigDecimal(setmealVo.getPrice());
        }
        cart.setName(name);
        cart.setImage(image);
        cart.setAmount(amount);

        Long count=shoppingCartMapper.queryCount(cart);
        if(count==0){
            //是新增
            cart.setNumber(1);
            cart.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.add(cart);
        }else {
            //是更新
            shoppingCartMapper.update(cart);
        }
    }

    @Override
    public List<ShoppingCart> queryList() {
        return shoppingCartMapper.queryByUserId(BaseContext.getEmpId());
    }

    @Override
    public void sub(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart cart=new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,cart);
        cart.setUserId(BaseContext.getEmpId());
        ShoppingCart result = shoppingCartMapper.query(cart);
        if (result == null) {
            return;
        }
        Integer count = result.getNumber();
        count--;
        if(count>0){
            cart.setNumber(count);
            shoppingCartMapper.reduceOnce(cart);
        }else {
            shoppingCartMapper.delete(cart);
        }
    }

    @Override
    public void clean() {
        shoppingCartMapper.deleteByUserId(BaseContext.getEmpId());
    }
}
